package edu.npu.cv.controller;


import edu.npu.cv.constant.TemplatePath;
import edu.npu.cv.dto.ActivityDTO;
import edu.npu.cv.entity.ActivityDO;
import edu.npu.cv.entity.ArticleDO;
import edu.npu.cv.service.ActivityService;
import edu.npu.cv.service.ArticleService;
import edu.npu.cv.utils.MarkDownUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lelechang
 * @description: 控制器层
 * @version: V1.0
 */
@RestController
@RequestMapping(TemplatePath.S + TemplatePath.ACTIVITIES)
public class ActivityController {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private ArticleService articleService;

    /**
     * 从数据库获取活动列表
     */
    private List<ArticleDO> getactivityList(){
        return activityService.findActivity();
    }

    /**
     * 查询所有活动
     * @return
     */
    @GetMapping
    public ModelAndView list(Model model){
        List<ActivityDTO> activityDTOList = new ArrayList<>();
        List<ArticleDO> articleDOList = getactivityList();
        for (int i=0;i<articleDOList.size();i++){
            activityDTOList.add(i,ActivityDTO.ActivityDO2DTO(articleDOList.get(i)));
        }
        model.addAttribute("activityList",activityDTOList);
        return new ModelAndView("activity","activityModel",model);
    }

    /**
     * 根据ID查询活动具体内容
     * @return
     */
    @GetMapping("{id}")
    public ModelAndView view(@PathVariable("id") String id, Model model){
        ArticleDO activity = articleService.getArticleDOById(Integer.parseInt(id));
//        activity.setContent(MarkDownUtil.mdToHtml(activity.getContent()));
//        Date time = activity.getCreateTime();
//        System.out.println(time);
//        //把年份截取出来
//        DateFormat df = new SimpleDateFormat("yyyy");
//        String year = df.format(time);
//        //activity.setCreateTime(year);
//        System.out.println(year);
        ActivityDTO activityDTO = ActivityDTO.ActivityDO2DTO(activity);
        model.addAttribute("activity",activityDTO);
        return new ModelAndView("activityview","activityModel",model);
    }
}
